﻿using System;
using System.Data;
using System.Data.SqlClient;

namespace Dapper
{
    /// <summary>
    ///     SqlCommandDecorator的装饰者
    /// </summary>
    /// <typeparam name="T"></typeparam>
    public class SqlCommandDecorator<T> : IDbCommand where T : IDbCommand
    {
        private readonly IDbCommand _decorator;

        /// <summary>
        ///     构造函数
        /// </summary>
        /// <param name="decorator"></param>
        public SqlCommandDecorator(T decorator)
        {
            _decorator = decorator;

            #region 加上set xact_abort on

            var sqlConnection = decorator.Connection as SqlConnection;
            if (sqlConnection != null)
                if (!decorator.CommandText.Contains("set xact_abort on"))
                {
                    var realcommandText = $@"set xact_abort on;{Environment.NewLine}{decorator.CommandText}";
                    _decorator.CommandText = realcommandText;
                }

            #endregion


        }

        public void Dispose()
        {
            _decorator.Dispose();
        }

        public void Prepare()
        {
            _decorator.Prepare();
        }

        public void Cancel()
        {
            _decorator.Cancel();
        }

        public IDbDataParameter CreateParameter()
        {
            return _decorator.CreateParameter();
        }

        public int ExecuteNonQuery()
        {
            var result = _decorator.ExecuteNonQuery();

            return result;
        }

        public IDataReader ExecuteReader()
        {
            var result = _decorator.ExecuteReader();

            return result;
        }

        public IDataReader ExecuteReader(CommandBehavior behavior)
        {
            var result = _decorator.ExecuteReader(behavior);

            return result;
        }

        public object ExecuteScalar()
        {
            var result = _decorator.ExecuteScalar();

            return result;
        }

        public IDbConnection Connection
        {
            get { return _decorator.Connection; }
            set { _decorator.Connection = value; }
        }

        public IDbTransaction Transaction
        {
            get { return _decorator.Transaction; }
            set { _decorator.Transaction = value; }
        }

        public string CommandText
        {
            get { return _decorator.CommandText; }
            set { _decorator.CommandText = value; }
        }

        public int CommandTimeout
        {
            get { return _decorator.CommandTimeout; }
            set { _decorator.CommandTimeout = value; }
        }

        public CommandType CommandType
        {
            get { return _decorator.CommandType; }
            set { _decorator.CommandType = value; }
        }

        public IDataParameterCollection Parameters => _decorator.Parameters;

        public UpdateRowSource UpdatedRowSource
        {
            get { return _decorator.UpdatedRowSource; }
            set { _decorator.UpdatedRowSource = value; }
        }

        ///// <summary>
        /////     记录运行日志
        ///// </summary>
        ///// <param name="command"></param>
        //private void Log(IDbCommand command)
        //{
        //    #region 记录日志

        //    var param = string.Empty;
        //    try
        //    {
        //        param = JsonConvert.SerializeObject(command.Parameters, Formatting.Indented);
        //    }
        //    catch (Exception)
        //    {
        //        // ignored
        //    }


        //    SqlTraceProvidor.LogInfo(new SqlLogEntity
        //    {
        //        LogTime = DateTime.Now,
        //        QueryId = Guid.NewGuid().ToString("N"),
        //        Sql = command.CommandText,
        //        Param = param
        //    });

        //    #endregion
        //}
    }
}